package mr.go.yaxc.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.net.URL;

import mr.go.yaxc.Actions;
import mr.go.yaxc.XmlConfigurator;
import mr.go.yaxc.tests.test1.Bean;
import mr.go.yaxc.tests.test1.BeanRepository;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Test1 {

    @BeforeClass
    public static void setUpBeforeClass()
            throws Exception {
        xmlUrl = Test1.class.getClassLoader().getResource("test1.xml");
        if (xmlUrl == null) {
            throw new IllegalStateException("No xml file");
        }
    }

    @AfterClass
    public static void tearDownAfterClass()
            throws Exception {
        Actions.resetConfiguration();
    }
    private XmlConfigurator configurator;

    @Before
    public void setUp()
            throws Exception {
        configurator = new XmlConfigurator();
        configurator.parse(xmlUrl);
    }

    @Test
    public void testSimplePropertySetters()
            throws Exception {
        Bean bean = BeanRepository.getBeans()[0];
        assertEquals(5,
                     bean.getIntValue());
        assertEquals("abc",
                     bean.getStringValue());
        assertTrue(bean.isBooleanValue());
        assertEquals(0.5d,
                     bean.getDoubleValue(),
                     0d);
        assertEquals(1.25f,
                     bean.getFloatValue(),
                     0d);
        assertArrayEquals(new String[]{"a",
                                       "b",
                                       "c"},
                          bean.getList().toArray());
    }

    @Test
    public void testListCreationAndMethodCall()
            throws Exception {
        Bean bean = BeanRepository.getBeans()[0];
        assertArrayEquals(new String[]{"a",
                                       "b",
                                       "c"},
                          bean.getList().toArray());
    }
    private static URL xmlUrl;
}
